﻿using System;
using System.Windows.Forms;
using System.Xml;

using Core;
using ClassLibrary;

namespace TestProject
{
    public partial class GeneTableEdit : Form
    {
        private Model model;            
        private GeneTable gen_t;
        private GeneType gen = null;
        public GeneTableEdit(Model m,GeneTable g)
        {
            InitializeComponent();
            gen_t = g;
            model = m;
        }

        private void canc_butt_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void update()
        {
            gene_box.Items.Clear();
            gen_t = new GeneTable(gen_t.path);
            name_box.Text = gen_t.name;
            if (gen_t.gene_table != null)
            {
                foreach (GeneType g in gen_t.gene_table)
                {
                    gene_box.Items.Add(g.name);
                }
            }
        }
        private void GeneTableEdit_Load(object sender, EventArgs e)
        {
            update();
        }

        private void gene_box_SelectedIndexChanged(object sender, EventArgs e)
        {            
            foreach (GeneType gg in gen_t.gene_table)
            {
                if ((string)gene_box.SelectedItem==gg.name)
                    gen=gg;
            }
            amount_box.Value = gen.amount_alelles; 
        }

        private void add_butt_Click(object sender, EventArgs e)
        {
            NewGene new_gene = new NewGene(gen_t);
            if (new_gene.ShowDialog(this) == DialogResult.OK)
                update();
        }

        private void remove_butt_Click(object sender, EventArgs e)
        {            
            if (!Functions.gene_is_used(gen_t,model.model_chromosome_gene_links,gen))
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(System.IO.File.ReadAllText(gen_t.path));
                foreach (XmlNode node in doc.GetElementsByTagName("gene"))
                {
                    if (gen.name == node.Attributes[1].Value)
                    {
                        node.ParentNode.RemoveChild(node);
                        break;
                    }
                }
                doc.DocumentElement.Attributes[1].Value = (Convert.ToInt32(doc.DocumentElement.Attributes[1].Value) - 1).ToString(); 
                doc.Save(gen_t.path);
                gene_box.Items.Remove(gen.name);
                amount_box.Value = 0;
                gene_box.SelectedItem = null;
            }
        }               
        private void ok_butt_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(gen_t.path));
            doc.DocumentElement.Attributes[0].Value = name_box.Text;                        
            doc.Save(gen_t.path);
            this.Close();
        }

        private void set_butt_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(gen_t.path));
            if (amount_box.IsDecremented())
            {
                for (int i = (int)amount_box.Value + 1; i <= (amount_box.Value + amount_box.difference); i++)
                {
                    if (!Functions.allele_is_used(model.model_species, gen, i))
                    {
                        foreach (XmlNode node in doc.GetElementsByTagName("gene"))
                        {
                            if (node.Attributes[1].Value == (string)gene_box.SelectedItem)
                            {
                                node.Attributes[2].Value = amount_box.Value.ToString();
                                break;
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Can not delete allele #" + i.ToString() + " because it is used.");
                        return;
                    }
                }
            }
            else
            {
                foreach (XmlNode node in doc.GetElementsByTagName("gene"))
                {
                    if (node.Attributes[1].Value == (string)gene_box.SelectedItem)
                    {
                        node.Attributes[2].Value = amount_box.Value.ToString();
                        break;
                    }
                }
            }
            doc.Save(gen_t.path);
            MessageBox.Show("The number was set.");
        }

        private void ren_butt_Click(object sender, EventArgs e)
        {
            if (gene_box.SelectedItem != null)
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(System.IO.File.ReadAllText(gen_t.path));
                foreach (XmlNode node in doc.GetElementsByTagName("gene"))
                {
                    if (node.Attributes[1].Value == (string)gene_box.SelectedItem)
                    {
                    Rename:
                        RenameForm renf = new RenameForm();
                        if (renf.ShowDialog() == DialogResult.OK)
                        {
                            foreach (XmlNode n in doc.GetElementsByTagName("gene"))
                            {
                                if ((n.Attributes[1].Value == Data.new_name) && (node.Attributes[1].Value != Data.new_name))
                                {
                                    MessageBox.Show("A gene with this name already exists. Please, choose another name.");
                                    goto Rename;
                                }
                            }
                            node.Attributes[1].Value = Data.new_name;
                        }
                        break;
                    }
                }
                doc.Save(gen_t.path);
                update();
            }
        }
    }
}
